<?php

namespace app\admin\common;


use think\Db;
use think\Exception;
use think\Model;


class ModelBase extends Model
{
    // 设置当前模型的数据库连接
    protected $connection = [
        // 数据库表前缀
        'prefix'      => 'so_',
    ];

    private static $errorMsg;

    const DEFAULT_ERROR_MSG = '操作失败,请稍候再试!';

    protected $insert=["create_time","time_formt","ip"];

    protected $params;

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated
    }

    // 请求参数


    /**
     * @return mixed
     * @author: hhygyl
     * @name: setIpAttr
     * @describe:
     */
    protected function setIpAttr()
    {
        return request()->ip();
    }


    /**
     * @return int
     * @author: hhygyl
     * @name: setCreateTimeAttr
     * @describe:
     */
    public function setTimeFormtAttr(){
        return date("Y-m-d H:i:s",time());
    }
    /**
     * @return int
     * @author: hhygyl
     * @name: setCreateTimeAttr
     * @describe:
     */
    public function setCreateTimeAttr(){
        return time();
    }



    /**
     * @param string $errorMsg
     * @param bool $rollback
     * @return bool
     * @author: hhygyl <hhygyl520@qq.com>
     * @name: setErrorInfo
     * @describe:设置错误信息
     */
    protected static function setErrorInfo($errorMsg = self::DEFAULT_ERROR_MSG,$rollback = false)
    {
        if($rollback) self::rollbackTrans();
        self::$errorMsg = $errorMsg;
        return false;
    }


    /**
     * @param string $defaultMsg
     * @return string
     * @author: hhygyl <hhygyl520@qq.com>
     * @name: getErrorInfo
     * @describe:获取错误信息
     */
    public static function getErrorInfo($defaultMsg = self::DEFAULT_ERROR_MSG)
    {
        return !empty(self::$errorMsg) ? self::$errorMsg : $defaultMsg;
    }

    /**
     * 开启事务
     */
    public static function beginTrans()
    {
        Db::startTrans();
    }

    /**
     * 提交事务
     */
    public static function commitTrans()
    {
        Db::commit();
    }

    /**
     * 关闭事务
     */
    public static function rollbackTrans()
    {
        Db::rollback();
    }



    /**
     * @param $res
     * @author: hhygyl <hhygyl520@qq.com>
     * @name: checkTrans
     * @describe:根据结果提交滚回事务
     */
    public static function checkTrans($res)
    {
        if($res){
            self::commitTrans();
        }else{
            self::rollbackTrans();
        }
    }



    /**
     * @param $data
     * @return array
     * @author: hhygyl <jackhhy520@qq.com>
     * @name: ChangeData
     * @describe:添加编辑数据
     */
    public function ChangeData($data){
       try{
           //dump($data);exit;
           if(isset($data['id']) && !empty($data['id'])){
              $res=$this->getModel()->isUpdate(true)->allowField(true)->save($data);
           }
           elseif(isset($data['tid']) && !empty($data['tid'])){
               $res=$this->getModel()->isUpdate(true)->allowField(true)->save($data);
           }
           else{
               $res=$this->getModel()->isUpdate(false)->allowField(true)->save($data);
               //echo $this->getModel()->getLastSql();exit;
           }

           if($res!==false){
             return  ["code"=>1,'id'=>$this->getModel()->getLastInsID()];
           }else{
             return  ["code"=>0,'msg'=>$this->getError()];
           }

       }catch (Exception $exception){
           return  ["code"=>0,'msg'=>$exception->getMessage()];
       }

    }


}